package service.Imp;

import Message.DTO.ShoppingCartDTO;
import Message.entity.Dish;
import Message.entity.Setmeal;
import Message.entity.ShoppingCart;
import Message.vo.DishVO;
import constant.BaseContext;
import mapper.DishMapper;
import mapper.SetmealMapper;
import mapper.ShoppingCartMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.ShoppingCartService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    
    @Autowired
    private DishMapper dishMapper;
    
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<ShoppingCart> list() {
        // 注意：只能查看自己名下的购物车
        ShoppingCart shoppingCart = new ShoppingCart();

        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //创建ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // copy属性值
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //1.判断商品是否已经存在购物车 --条件 dishId+dishFlavor+userId
        //只查看当前用户自己的购物车

        // 设置购物车商品关联的用户ID，从当前登录用户上下文中获取
        shoppingCart.setUserId(BaseContext.getCurrentId());

        ShoppingCart Cart =shoppingCartMapper.selectBy(shoppingCart);

        if (Cart == null){//代表购物车中没有数据
           //补充缺失的属性
           //判断是新增套餐还是新增菜品
            if(shoppingCart.getDishId()!=null){
              DishVO dish =  dishMapper.SelectDish(shoppingCartDTO.getDishId());
              shoppingCart.setName(dish.getName());
              shoppingCart.setAmount(dish.getPrice());
              shoppingCart.setImage(dish.getImage());
            }else{
                //代表新增的是套餐
                Setmeal setmeal = setmealMapper.SelectByid(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);   //数量---》到底是1还是加1？判断该商品是否已经存在购物车
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 3.将商品数据存入到shopping_cart表中
            shoppingCartMapper.insert(shoppingCart);

        }else {//代表购物车有该商品数据
            // 4.将原来的购物车商品数量+1,调用mapper更新方法
            // 处理数量为null的情况，如果为null则默认为0
            Integer currentNumber = Cart.getNumber();
            if (currentNumber == null) {
                currentNumber = 0;
            }
            Cart.setNumber(currentNumber + 1);
            shoppingCartMapper.update(Cart);
        }

        // 最终目的：将用户添加的商品，存入到购物车表中shopping_cart表
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //创建ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // copy属性值
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //1.判断商品是否已经存在购物车 --条件 dishId+dishFlavor+userId
        //只查看当前用户自己的购物车

        // 设置购物车商品关联的用户ID，从当前登录用户上下文中获取
        shoppingCart.setUserId(BaseContext.getCurrentId());

        ShoppingCart Cart = shoppingCartMapper.selectBy(shoppingCart);
        if (Cart == null) {
            return; // 当前商品不在购物车，直接返回
        }

        Integer currentNumber = Cart.getNumber();
        if (currentNumber == null || currentNumber <= 1) {
            // 数量为1或空，则直接删除该行
            shoppingCartMapper.delete(shoppingCart);
        } else {
            // 数量大于1，做减一并更新
            Cart.setNumber(currentNumber - 1);
            shoppingCartMapper.update(Cart);
        }
    }

    @Override
    public void clean() {

       shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
}